package team3132.utils;

import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.tables.TableKeyNotDefinedException;

/**
 * Create a wrapper around an value stored in the network table.
 * If the value is not present in the table, an exception is thrown on startup.
 */
public class NetworkTableValue implements Value {

  private final String key;

  public NetworkTableValue(String key) throws TableKeyNotDefinedException {
    this(key, true);
  }

  protected NetworkTableValue(String key, boolean checkValueExists)
          throws TableKeyNotDefinedException {
    this.key = key;
    if (checkValueExists) {
      NetworkTableHelper.getTable().getValue(key);
    }
  }

  protected boolean isValueSet() {
    NetworkTable table = NetworkTableHelper.getTable();
    return table.containsKey(key);
  }

  public double getDoubleValue() throws TableKeyNotDefinedException {
    NetworkTable table = NetworkTableHelper.getTable();
    return table.getNumber(key);
  }

  public int getIntValue() throws TableKeyNotDefinedException {
    NetworkTable table = NetworkTableHelper.getTable();
    return (int) table.getNumber(key);
  }

  public String getStringValue() throws TableKeyNotDefinedException {
    NetworkTable table = NetworkTableHelper.getTable();
    return table.getString(key);
  }
}
